﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace HelperLibrary
{
    public class SqlHelper
    {
        /// <summary>
        /// 真值
        /// </summary>
        public static string trueStr = " 1=1 ";
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static string DbType = "mysql";
        /// <summary>
        /// 数据库名
        /// </summary>
        public static string Database = "";

        /// <summary>
        /// 获取表名称
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetTableNameByModelType<T>()
        {
            Type t = typeof(T);
            DescriptionAttribute[] attributes = (DescriptionAttribute[])t.GetCustomAttributes(typeof(DescriptionAttribute), false);
            string tableName = string.Empty;
            if (attributes == null || !attributes.Any())
            {
                tableName = t.Name;
            }
            else
            {
                tableName = attributes[0].Description;
            }
            if (DbType == "dm")
            {
                return $"{Database}.\"{tableName}\"";
            }
            else if (DbType == "rdjc")
            {
                return $"{Database}.\"{tableName}\"";
            }
            else
            {
                return $"`{tableName}`";
            }
        }

        /// <summary>
        /// 获取In 条件 ('item1', 'item2', 'item3')
        /// </summary>
        /// <param name="strList"></param>
        /// <returns></returns>
        public static string GetInCondition(List<string> strList)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("(");
            for (int i = 0; i < strList.Count; i++)
            {
                sb.Append("'");
                sb.Append(strList[i]);
                sb.Append("'");
                if (i != strList.Count - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append(")");

            return sb.ToString();
        }

        /// <summary>
        /// 获取In 条件 ('item1', 'item2', 'item3')
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string GetInCondition(List<int> ids)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("(");
            for (int i = 0; i < ids.Count; i++)
            {
                sb.Append(ids[i]);
                if (i != ids.Count - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append(")");

            return sb.ToString();
        }

        /// <summary>
        /// 构建数据插入语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="returnAutoId"></param>
        /// <param name="addWithFixId"></param>
        /// <returns></returns>
        public static string BuildInsertSql<T>(bool returnAutoId = false, bool addWithFixId = false)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            StringBuilder sb = new StringBuilder();
            sb.Append($"INSERT INTO {tableName} (");
            bool isFirst = true;

            foreach (PropertyInfo p in t.GetProperties())
            {
                if (!addWithFixId && p.Name.ToUpper() == "ID")
                {
                    continue;
                }
                if (isFirst)
                {
                    isFirst = false;
                    sb.Append($"`{p.Name}`");
                }
                else
                {
                    sb.Append($",`{p.Name}`");
                }
            }

            sb.Append(")VALUES(");

            isFirst = true;
            foreach (PropertyInfo p in t.GetProperties())
            {
                if (!addWithFixId && p.Name.ToUpper() == "ID")
                {
                    continue;
                }
                if (isFirst)
                {
                    isFirst = false;
                    sb.Append($"@{p.Name}");
                }
                else
                {
                    sb.Append($",@{p.Name}");
                }
            }
            sb.Append(")");

            if (returnAutoId)
            {
                sb.Append(";SELECT LAST_INSERT_ID();");
            }
            if (DbType == "dm")
            {
                return sb.ToString().Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sb.ToString().Replace("`", "\"");
            }
            else
            {
                return sb.ToString();
            }
        }

        /// <summary>
        /// 构建批量插入语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tableName"></param>
        /// <param name="list"></param>
        /// <param name="withFixId">是否插入固定id</param>
        /// <returns></returns>
        public static string BuildRealBatchInsertSql<T>(List<T> list, bool withFixId = false)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            StringBuilder sb = new StringBuilder();
            sb.Append($"INSERT INTO {tableName} (");
            bool isFirst = true;

            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo p in properties)
            {
                if (!withFixId && p.Name.ToUpper() == "ID")
                {
                    continue;
                }
                if (isFirst)
                {
                    isFirst = false;
                    sb.Append($"`{p.Name}`");
                }
                else
                {
                    sb.Append($",`{p.Name}`");
                }
            }

            sb.Append(")VALUES");

            foreach (T item in list)
            {
                sb.Append("(");
                isFirst = true;
                foreach (PropertyInfo p in properties)
                {
                    if (!withFixId && p.Name.ToUpper() == "ID")
                    {
                        continue;
                    }
                    object o = t.GetProperty(p.Name).GetValue(item, null);
                    if (isFirst)
                    {
                        if (o == null)
                        {
                            sb.Append("NULL");
                        }
                        else
                        {
                            string value = Convert.ToString(o);
                            if (o.GetType() == typeof(bool))
                            {
                                sb.Append($"{value.ToUpper()}");
                            }
                            else if (o.GetType() == typeof(DateTime))
                            {
                                sb.Append($"'{Convert.ToDateTime(value).ToYMDHMS()}'");
                            }
                            else
                            {
                                sb.Append($"'{value}'");
                            }
                        }
                        isFirst = false;
                    }
                    else
                    {
                        if (o == null)
                        {
                            sb.Append(",NULL");
                        }
                        else
                        {
                            string value = Convert.ToString(o);
                            if (o.GetType() == typeof(bool))
                            {
                                sb.Append($",{value.ToUpper()}");
                            }
                            else if (o.GetType() == typeof(DateTime))
                            {
                                sb.Append($",'{Convert.ToDateTime(value).ToYMDHMS()}'");
                            }
                            else
                            {
                                sb.Append($",'{value}'");
                            }
                        }
                    }
                }

                sb.Append("),");
            }

            if (DbType == "dm")
            {
                return sb.ToString().TrimEnd(',').Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sb.ToString().TrimEnd(',').Replace("`", "\"");
            }
            else
            {
                return sb.ToString().TrimEnd(',');
            }
        }

        /// <summary>
        /// 构建更新语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ignoreFields">需要忽略的字段</param>
        /// <returns></returns>
        public static string BuildUpdateSql<T>(List<string> ignoreFields = null)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            StringBuilder sb = new StringBuilder();
            sb.Append($"UPDATE {tableName} SET ");
            bool isFirst = true;
            List<string> ignoreProperty = new List<string>() { "ID" };
            var idPropertyName = "";
            if (ignoreFields != null && ignoreFields.Any())
            {
                ignoreProperty.AddRange(ignoreFields.Select(i => i.ToUpper()));
            }
            foreach (PropertyInfo p in t.GetProperties())
            {
                if (ignoreProperty.Contains(p.Name.ToUpper()))
                {
                    idPropertyName = p.Name;
                    continue;
                }
                if (isFirst)
                {
                    isFirst = false;
                    sb.Append($"`{p.Name}` = @{p.Name}");
                }
                else
                {
                    sb.Append($",`{p.Name}` = @{p.Name}");
                }
            }

            sb.Append($" WHERE `Id` = @{idPropertyName}");
            if (DbType == "dm")
            {
                return sb.ToString().Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sb.ToString().Replace("`", "\"");
            }
            else
            {
                return sb.ToString();
            }
        }

        /// <summary>
        /// 构建批量插入语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tableName"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string BuildBatchUpdateSql<T>(List<T> list)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            List<string> majorPropertyNames = new List<string>() { "ID" };
            List<PropertyInfo> properties = t.GetProperties().ToList();
            PropertyInfo marjorProperty = properties
                .First(p => majorPropertyNames.Contains(p.Name.ToUpper()));
            StringBuilder sb = new StringBuilder();
            foreach (T item in list)
            {
                bool isFirst = true;
                sb.Append($" UPDATE {tableName} SET ");
                foreach (PropertyInfo p in properties)
                {
                    if (majorPropertyNames.Contains(p.Name.ToUpper()))
                    {
                        continue;
                    }

                    object o = t.GetProperty(p.Name).GetValue(item, null);
                    if (isFirst)
                    {
                        isFirst = false;
                        if (o == null)
                        {
                            sb.Append($"`{p.Name}` = NULL");
                        }
                        else
                        {
                            string value = Convert.ToString(o);
                            if (o.GetType() == typeof(bool))
                            {
                                sb.Append($"`{p.Name}` = {value.ToUpper()}");
                            }
                            else if (o.GetType() == typeof(DateTime))
                            {
                                sb.Append($"`{p.Name}` = '{Convert.ToDateTime(value).ToYMDHMS()}'");
                            }
                            else 
                            {
                                sb.Append($"`{p.Name}` = '{value}'");
                            }
                        }
                    }
                    else
                    {
                        if (o == null)
                        {
                            sb.Append($", `{p.Name}` = NULL");
                        }
                        else
                        {
                            string value = Convert.ToString(o);
                            if (o.GetType() == typeof(bool))
                            {
                                sb.Append($", `{p.Name}` = {value.ToUpper()}");
                            }
                            else if (o.GetType() == typeof(DateTime))
                            {
                                sb.Append($", `{p.Name}` = '{Convert.ToDateTime(value).ToYMDHMS()}'");
                            }
                            else
                            { 
                                sb.Append($", `{p.Name}` = '{value}'");
                            }
                        }
                    }
                }

                string majorKeyVal = t.GetProperty(marjorProperty.Name).GetValue(item, null).ToString();
                sb.Append($" WHERE Id = {majorKeyVal};");
            }

            if (DbType == "dm")
            {
                return sb.ToString().Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sb.ToString().Replace("`", "\"");
            }
            else
            {
                return sb.ToString();
            }
        }

        /// <summary>
        /// 构造删除Sql
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string BuildDeleteSql<T>(List<int> ids)
        {
            string tableName = GetTableNameByModelType<T>();
            var sql = $"DELETE FROM {tableName} WHERE `Id` IN {GetInCondition(ids)}";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造根据主键删除Sql
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string BuildDeleteByKeySql<T>()
        {
            string tableName = GetTableNameByModelType<T>();
            Type t = typeof(T);
            var idPropertyName = "";
            var idProperty = t.GetProperties().FirstOrDefault(x => x.Name.ToUpper() == "ID");
            if (idProperty != null)
            {
                idPropertyName = idProperty.Name;
            }
            var sql = $"DELETE FROM {tableName} WHERE `Id` = @{idPropertyName} ";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造软删除Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string BuildSoftDeleteSql<T>(List<int> ids)
        {
            string tableName = GetTableNameByModelType<T>();
            var sql = $"UPDATE {tableName} SET `Deleted` = 1 WHERE `Id` IN {GetInCondition(ids)}";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造根据列名删除Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string BuildPropertyDeleteSql<T>(string propertyName)
        {
            string tableName = GetTableNameByModelType<T>();
            var sql = $"DELETE FROM {tableName} WHERE `{propertyName}` = @Value";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造删除SQL
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public static string BuildDeleteSqlByWhere<T>(string whereStr)
        {
            string tableName = GetTableNameByModelType<T>();
            var sql = $"DELETE FROM {tableName} WHERE {WhereFormat(whereStr)}";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造删除所有Sql
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string BuildDeleteAllSql<T>()
        {
            string tableName = GetTableNameByModelType<T>();
            return $"TRUNCATE TABLE {tableName}";
        }

        /// <summary>
        /// 构造根据单条件获取Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string BuildGetSql<T>(string propertyName, bool limit1 = true)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            var sql = "";
            if (limit1)
            {
                sql = $"SELECT * FROM {tableName} WHERE `{propertyName}` = @Value ORDER BY Id DESC LIMIT 1";
            }
            else
            {
                sql = $"SELECT * FROM {tableName} WHERE `{propertyName}` = @Value ORDER BY Id DESC";
            }
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造根据多条件获取Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string BuildGetByPropertiesSql<T>(List<string> propertyNames, bool limit1 = true, bool isAnd = true)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            StringBuilder sb = new StringBuilder();
            sb.Append($" SELECT * FROM {tableName} WHERE ");
            string logicalCondition = "OR";
            if (isAnd)
            {
                logicalCondition = "AND";
            }

            for (int i = 0; i < propertyNames.Count; i++)
            {
                if (i == 0)
                {
                    sb.Append($" `{propertyNames[i]}` = @{propertyNames[i]} ");
                }
                else
                {
                    sb.Append($" {logicalCondition} `{propertyNames[i]}` = @{propertyNames[i]} ");
                }
            }

            if (limit1)
            {
                sb.Append(" LIMIT 1 ");
            }
            if (DbType == "dm")
            {
                return sb.ToString().Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sb.ToString().Replace("`", "\"");
            }
            else
            {
                return sb.ToString();
            }
        }

        /// <summary>
        /// 查询所有Sql
        /// </summary>
        /// <returns></returns>
        public static string BuildGetAllSql<T>()
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            return $"SELECT * FROM {tableName}";
        }

        /// <summary>
        /// 查询所有Sql
        /// </summary>
        /// <returns></returns>
        public static string BuildGetByWhere<T>(string whereStr, bool limit1 = true)
        {
            string tableName = GetTableNameByModelType<T>();
            var sql = "";
            if (limit1)
            {
                sql = $"SELECT * FROM {tableName} WHERE {WhereFormat(whereStr)} LIMIT 1";
            }
            else
            {
                sql = $"SELECT * FROM {tableName} WHERE {WhereFormat(whereStr)}";
            }
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 根据主键查询Sql
        /// </summary>
        /// <returns></returns>
        public static string BuildGetByIdSql<T>()
        {
            Type t = typeof(T);
            var idPropertyName = "";
            var idProperty = t.GetProperties().FirstOrDefault(x => x.Name.ToUpper() == "ID");
            if (idProperty != null)
            {
                idPropertyName = idProperty.Name;
            }
            string tableName = GetTableNameByModelType<T>();
            var sql = $"SELECT * FROM {tableName} WHERE `Id` = @{idPropertyName} LIMIT 1";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 根据ID集合查询Sql
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string BuildGetByIdsSql<T>(List<int> ids)
        {
            string tableName = GetTableNameByModelType<T>();
            var sql = $"SELECT * FROM {tableName} WHERE `Id` IN {GetInCondition(ids)}";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造单表分页Sql
        /// </summary>
        /// <returns></returns>
        public static string BuildSingleTablePageQuerySql<T>(string where, string orderByFiledName, bool isDesc, int pageIndex, int pageSize)
        {
            // 获取表名
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            // 计算分页开始行号
            int start = (pageIndex - 1) * pageSize;
            // 拼接排序
            string descStr = string.Empty;
            if (isDesc)
            {
                descStr = "DESC";
            }
            string orderBy = $" ORDER BY {orderByFiledName} {descStr} ";
            var sql = $" SELECT * FROM {tableName} WHERE {WhereFormat(where)} {orderBy} LIMIT {start},{pageSize} ";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// 构造单表分页Sql
        /// </summary>
        /// <returns></returns>
        public static string BuildSingleTableCoutSql<T>(string where)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            var sql = $" SELECT COUNT(1) FROM {tableName} WHERE {WhereFormat(where)} ";
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// where 条件格式化
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        private static string WhereFormat(string where)
        {
            if (string.IsNullOrEmpty(where))
            {
                where = "1=1";
            }
            else
            {
                where = where.TrimStart();
                if (where.ToUpper().IndexOf("WHERE") == 0)
                {
                    where = where.Substring(5);
                }
            }
            return where;
        }

        /// <summary>
        /// 构造更新多字段Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string BuildUpdateByPropertiesSql<T>(List<string> propertyNames)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            StringBuilder sb = new StringBuilder();
            sb.Append($" UPDATE {tableName} SET ");
            var idPropertyName = "";
            var idProperty = t.GetProperties().FirstOrDefault(x => x.Name.ToUpper() == "ID");
            if (idProperty != null)
            {
                idPropertyName = idProperty.Name;
            }
            for (int i = 0; i < propertyNames.Count; i++)
            {
                if (i == propertyNames.Count-1)
                {
                    sb.Append($" `{propertyNames[i]}` = @{propertyNames[i]} ");
                }
                else
                {
                    sb.Append($" `{propertyNames[i]}` = @{propertyNames[i]},");
                }
            }
            sb.Append($" WHERE `Id` = @{idPropertyName} ");
            if (DbType == "dm")
            {
                return sb.ToString().Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sb.ToString().Replace("`", "\"");
            }
            else
            {
                return sb.ToString();
            }
        }

        /// <summary>
        /// 获取单条数据（主键倒序）Sql
        /// </summary>
        /// <returns></returns>
        public static string BuildGetSingleSql<T>(bool desc)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            string descStr = string.Empty;
            if (desc)
            {
                descStr = "DESC";
            }
            return $"SELECT * FROM {tableName} ORDER BY `Id` {descStr} LIMIT 1";
        }

        /// <summary>
        /// 构造判断记录是否存在Sql
        /// </summary>
        public static string BuildIsExistSql<T>(int id, string propertyName, bool delete)
        {
            Type t = typeof(T);
            string tableName = GetTableNameByModelType<T>();
            string sql = $"select * from {tableName} where {propertyName}=@Value";
            if (delete)
            {
                sql += " and deleted=0";
            }
            if (id == 0)
            {
                sql += " LIMIT 1";
            }
            else
            {
                sql += " and id<>@id LIMIT 1";
            }
            if (DbType == "dm")
            {
                return sql.Replace("`", "\"").Replace("@", ":");
            }
            else if (DbType == "rdjc")
            {
                return sql.Replace("`", "\"");
            }
            else
            {
                return sql;
            }
        }

        /// <summary>
        /// Sql 关键字替换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string PercentReplace(string str)
        {
            if (str.Contains("%"))
            {
                str = str.Replace("%", "\\%");
            }
            return str;
        }

        /// <summary>
        /// 构造列表字符串 模糊查询条件
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string BuildFuzzyQueryOrCondition<T>(List<T> list, string fileName)
        {
            string conditionStr = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 0)
                {
                    conditionStr += $" INSTR({fileName},',{list[i]},') ";
                }
                else
                {
                    conditionStr += $" OR INSTR({fileName},',{list[i]},') ";
                }
            }
            return conditionStr;
        }

        /// <summary>
        /// 构造列表字符串 模糊查询条件
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string BuildFuzzyQueryOrCondition2<T>(List<T> list, string fileName)
        {
            string conditionStr = string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 0)
                {
                    conditionStr += $" INSTR({fileName},'\"{list[i]}\":\"[]\"') ";
                }
                else
                {
                    conditionStr += $" OR INSTR({fileName},'\"{list[i]}\":\"[]\"') ";
                }
            }
            return conditionStr;
        }
    }
}
